package ws.sentiment;

import java.util.Arrays;
import java.util.List;

import model.Pair;
import play.libs.F.Promise;
import play.libs.ws.WS;
import play.libs.ws.WSResponse;
import property.Constants;
import property.DefaultStringValue;
import property.DynamicProperty;
import property.Key;

public class WsSentiment {

	public static Promise<WSResponse> login;
	private static long refresh = 0;
	
//	public static List<Pair> getPairs(){
//		return Arrays.asList(new Pair("111", "EUR/USD"), new Pair(
//				"112", "GBP/USD"), new Pair("113", "USD/JPY"), new Pair("114",
//				"USD/CHF"), new Pair("115", "AUD/USD"), new Pair("116",
//				"USD/CAD"), new Pair("170", "EUR/GBD"), new Pair("171",
//				"EUR/JPY"), new Pair("172", "GBP/JPY"));
//	}
	
	public static List<Pair> getPairs(WSResponse response){
		return Arrays.asList(new Pair("111", "EUR/USD",response), new Pair(
				"112", "GBP/USD",response), new Pair("113", "USD/JPY",response), new Pair("114",
				"USD/CHF",response), new Pair("115", "AUD/USD",response), new Pair("116",
				"USD/CAD",response), new Pair("170", "EUR/GBD",response), new Pair("171",
				"EUR/JPY",response), new Pair("172", "GBP/JPY",response));
	}
	
	
//	public static Promise<List<Pair>> getPair(){
//
//		return getLogin().map(resp->SentimentPair.pair(resp, pairs));
//		
////		return p.parallelStream().map(pair -> SentimentPair.pair(response, pair))
////				.filter(pair->filter(pair)).count();
//	}

	public static Promise<WSResponse> getLogin() {
		if (login == null
				|| (refresh + Long.parseLong(SentimentConfig.config().refreshTime())) < System.currentTimeMillis()) {
			login = WS
					.client()
					.url(SentimentConfig.config().url())
					.setQueryParameter("email",
							SentimentConfig.config().email())
					.setQueryParameter("password", SentimentConfig.config().password())
					.setMethod("POST").execute();
			refresh = System.currentTimeMillis();
		}
		return login;
	}

	public static class SentimentConfig {

		private static ISentimentConfig iSentimentConfig;

		public static ISentimentConfig config() {
			if (iSentimentConfig == null)
				iSentimentConfig = DynamicProperty
						.create(ISentimentConfig.class);
			return iSentimentConfig;
		}

		public static interface ISentimentConfig extends Constants {

			@DefaultStringValue("https://www.stockpair.com/session/authenticate?json")
			@Key("url")
			String url();

			@DefaultStringValue("michele.zanotto.1981@gmail.com")
			@Key("email")
			String email();

			@DefaultStringValue("QWEzxcasd123")
			@Key("password")
			String password();
			
			@DefaultStringValue("900000") // 15 minuts
			@Key("refreshTime")
			String refreshTime();

		}
	}
}
